﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Design.WebUserControls.Frame;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.WebUserControls.Lookup
{
    public partial class AssociatePremiseLookup : UserControl
    {
        public int ServiceId
        {
            get { return ViewState["ServiceId"] != null ? (int) ViewState["ServiceId"] : -1; }
            set { ViewState["ServiceId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["premiseFilter"] = ViewState["premiseFilter"] ?? new PremiseFilter();
            }
        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            var filter = ViewState["premiseFilter"] as PremiseFilter;
            if (filter == null)
            {
                return;
            }

            filter.LocationName = TextBoxLocationName.Text.Trim();
            GridViewPremise.DataBind();
        }

        protected void ButtonClear_Click(object sender, EventArgs e)
        {
            TextBoxLocationName.Text = string.Empty;
        }
        
        protected void ButtonSelect_Click(object sender, EventArgs e)
        {
            // Get selected premise id
            int selectedPremiseId;
            if (!int.TryParse(Request.Form["RadioButton"], out selectedPremiseId))
            {
                return;
            }

            // Add service-premise into db
            var newServicePremise = new ServicePremise();
            newServicePremise.PremiseId = selectedPremiseId;
            newServicePremise.ServiceId = ServiceId;
            newServicePremise.ProjectCode = TextBoxProjectCode.Text;
            ServicePremiseBO.AssociateServiceAndPremise(newServicePremise);
            GridViewPremise.DataBind();

            var parent = Parent as ServicePremiseFrame;
            if (parent != null)
            {
                parent.GridView_DataBind();
            }

            PanelContainer.Visible = false;
        }
            
        protected void ButtonClose_Click(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
        }

        protected void ObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["premiseFilter"] as PremiseFilter;
            if (filter != null)
            {
                filter.UnassociatedServiceId = ServiceId;
                e.InputParameters["filter"] = filter;
            }
        }

        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;

            TextBoxLocationName.Text = string.Empty;
            TextBoxProjectCode.Text = string.Empty;

            GridViewPremise.DataBind();
        }
    }
}